package com.yun.zf.admin.controller;

import com.yun.zf.common.admin.domain.SysDictData;
import com.yun.zf.common.admin.model.SysDicDataMo;
import com.yun.zf.common.admin.model.sys.SysDictDataAddMo;
import com.yun.zf.common.admin.model.sys.SysdictDataEditMo;
import com.yun.zf.common.annotation.LogPrint;
import com.yun.zf.common.vo.Response;
import com.yun.zf.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dict/data")
@Api(tags = "字典数据管理")
@Slf4j
public class SysDictDataController
{

    @Autowired
    private ISysDictDataService dictDataService;




    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/data/{dictCode}")
    @ApiOperation(value = "查询字典数据详情")
    @LogPrint(description = "查询字典数据详情")
    public Response<SysDictData> getInfoData(@PathVariable("dictCode") Long dictCode)
    {
        return Response.success(dictDataService.selectDictDataById(dictCode));
    }


    /*
    {
  "current": 1,
  "dictLabel": "",
  "dictType": "sys_oper_type",
  "pageSize": 10,
  "sort": {
    "createTime": "",
    "updateTime": ""
  },
  "status": "all"
}
     */
    @PostMapping(value = "/dictType")
    @ApiOperation(value = "字典值多条件查询列表")
    public Response dictType(@RequestBody SysDicDataMo dictType)
    {
        return Response.success(dictDataService.selectDictDataByType(dictType));
    }

    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @PostMapping("/add")
    @ApiOperation(value = "新增字典数据")
    @LogPrint(description = "新增字典数据")
    public Response add(@Validated @RequestBody SysDictDataAddMo dict)
    {
        // 验证  key 与type 确认唯一
      String dvalue =  dictDataService.getValue(dict.getDictType(),dict.getDictKey());
      if (StringUtils.isNotBlank(dvalue)){
          return  Response.error("字典数据已存在，请勿重复添加");
      }
        dictDataService.insertDictData(dict);
        return Response.success();
    }

    /**
     * 根据dict_type, id修改字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @PostMapping("/edit")
    @LogPrint(description = "修改字典数据")
    @ApiOperation(value = "修改字典数据")
    public Response edit(@Validated @RequestBody SysdictDataEditMo editMo)
    {
        dictDataService.updateDictData(editMo);
        return Response.success();
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @PostMapping("/removeDictData")
    @LogPrint(description = "删除字典数据")
    @ApiOperation(value = "删除字典数据")
    public Response removeDictData(@RequestBody Map<String,Long[]> map)
    {
        try {
            dictDataService.deleteDictDataByIds(map.get("dictCodes"));
            return Response.success();
        }catch (Exception e){

        }
        return Response.error();

    }



}
